import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jicaimao_flutter/common/constants.dart';
import 'package:jicaimao_flutter/entity/product_entity.dart';

class ProductPage extends StatefulWidget {
  final mProducts = <ProductBean>[
    ProductBean(8, 90, 98),
    ProductBean(10, 180, 246),
    ProductBean(15, 356, 750),
  ];

  @override
  _ProductPageState createState() => _ProductPageState();
}

class _ProductPageState extends State<ProductPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '产品',
          style: TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.w100,
            fontSize: 16.0,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        actions: <Widget>[
          IconButton(icon: Icon(Icons.smartphone), onPressed: null)
        ],
      ),
      body: RefreshIndicator(
          child: CustomScrollView(
            slivers: <Widget>[
              SliverList(
                delegate: SliverChildListDelegate(
                    widget.mProducts.map((ProductBean product) {
                  return ProductItem(
                    product: product,
                  );
                }).toList()),
              ),
              SliverSafeArea(
                  sliver: SliverToBoxAdapter(
                child: Padding(
                  padding: const EdgeInsets.only(top: 10.0),
                  child: Container(
                    height: 200.0,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Image.asset('images/icon_share.png'),
                        Text('敬请期待')
                      ],
                    ),
                    color: Colors.white,
                  ),
                ),
              ))
            ],
          ),
          onRefresh: () {}),
    );
  }
}

class ProductItem extends StatelessWidget {
  ProductItem({this.product, this.onTabCallback});

  final ProductBean product;
  final ValueChanged<void> onTabCallback;

  Future<Null> _handleTap(ProductBean product) async {
    await android_platForm
        .invokeMethod('startProductActivity', <String, dynamic>{
      'reward': product.reward.toString(),
      'regularProduct': product.regularProduct.toString(),
      'welfare': product.welfare.toString(),
    });
    print('_handleTap');
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 6.0),
      child: GestureDetector(
        onTap: () {
          _handleTap(product);
        },
        child: Container(
          decoration: BoxDecoration(
              color: Colors.white,
              border: Border(left: BorderSide(color: Colors.red, width: 4.0))),
          height: 72.0,
          child: Row(
            children: <Widget>[
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text.rich(TextSpan(
                        text: product.reward.toString(),
                        style: TextStyle(
                            color: Colors.red,
                            fontSize: 20.0,
                            fontWeight: FontWeight.w300),
                        children: <TextSpan>[
                          TextSpan(text: '%', style: TextStyle(fontSize: 14.0))
                        ])),
                    Text(
                      '奖励',
                      style: TextStyle(fontSize: 12.0),
                    )
                  ],
                ),
              ),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text.rich(TextSpan(
                        text: product.regularProduct.toString(),
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 18.0,
                            fontWeight: FontWeight.w300),
                        children: <TextSpan>[
                          TextSpan(text: '天', style: TextStyle(fontSize: 14.0))
                        ])),
                    Text(
                      '定期产品',
                      style: TextStyle(fontSize: 12.0),
                    )
                  ],
                ),
              ),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text.rich(TextSpan(
                        text: product.welfare.toString(),
                        style: TextStyle(
                            color: Colors.red,
                            fontSize: 18.0,
                            fontWeight: FontWeight.w300),
                        children: <TextSpan>[
                          TextSpan(
                              text: '(BSC)', style: TextStyle(fontSize: 14.0))
                        ])),
                    Text(
                      '福利',
                      style: TextStyle(fontSize: 12.0),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
